//*****************************************************************************
// JFab (http://code.google.com/p/jfab)
// Copyright (c) 2011-2012 JFab.org
// Admin jfab@jeffreythompson.net
//
// See the file "LICENSE.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//*****************************************************************************

package org.jfab.swingui.decorator;

import javax.swing.Icon;

import org.apache.commons.lang3.StringUtils;
import org.jfab.core.model.CGInterface;
import org.jfab.core.model.CGMethodDeclaration;
import org.jfab.core.model.CGObject;
import org.jfab.core.model.CGParameter;
import org.jfab.swingui.IconManager;
import org.jfab.swingui.editpane.CGMethodDeclarationEditPane;
import org.jfab.swingui.editpane.EditPane;

/**
 * Provides a decorator implementation for <code>CGMethodDeclaration</code>.
 * <p>
 * Possible children are:
 * <ul>
 * <li>parameter</li>
 * </ul>
 * </p>
 * 
 * @param <T> Component type.
 */
public class CGMethodDeclarationDecorator<T extends CGMethodDeclaration>
        extends CGObjectDecorator<T>
{
    /** Child selection values. */
    private static final String[] CHILD_SELECTION_VALUES = { "parameter", };

    /**
     * Construct this object with the given parameter.
     * 
     * @param component Component.
     * @param parent Parent.
     * @param iconManager Icon manager.
     */
    public CGMethodDeclarationDecorator(final T component,
            final Decorator<?> parent, final IconManager iconManager)
    {
        super(component, parent, iconManager);
    }

    @Override
    public void addNode(final String childTypeName)
    {
        if (StringUtils.isNotEmpty(childTypeName))
        {
            final CGMethodDeclaration parent = getComponent();
            final CGObject newChild = getModelFactory().create(childTypeName);

            if (newChild instanceof CGParameter)
            {
                parent.getParameters().add((CGParameter)newChild);
            }
        }
    }

    @Override
    public Object getChild(final int index)
    {
        return getComponent().getParameters().get(index);
    }

    @Override
    public int getChildCount()
    {
        return getComponent().getParameters().size();
    }

    @Override
    public String[] getChildSelectionValues()
    {
        return CHILD_SELECTION_VALUES;
    }

    @Override
    public EditPane<T> getEditPane()
    {
        @SuppressWarnings("unchecked")
        final EditPane<T> answer = (EditPane<T>)new CGMethodDeclarationEditPane<CGMethodDeclaration>(
                getComponent());

        return answer;
    }

    @Override
    public Icon getIcon()
    {
        return getIconManager().getMethodIcon();
    }

    @Override
    public int getIndexOfChild(final Object child)
    {
        return getComponent().getParameters().indexOf(child);
    }

    @Override
    public boolean isLeaf()
    {
        return getComponent().getParameters().isEmpty();
    }

    @Override
    public boolean removeNode()
    {
        final CGInterfaceDecorator parent = (CGInterfaceDecorator)getParent();
        final CGInterface cgInterface = parent.getComponent();

        return cgInterface.getMethods().remove(getComponent());
    }
}
